<!DOCTYPE HTML>
<script src="/js-test-resources/js-test.js"></script>
<script>
description("Test that WebSocket connections are throttled.");

window.jsTestIsAsync = true;

var socketCount = 300;
var openedCount = 0;
var closedCount = 0;

// Expected behavior:
// 1. A handshake to "ws://127.0.0.1:8880/delayed-handshake" is started.
//    This stays pending for 1 second.
// 2. Another 299 WebSockets to "ws://127.0.0.1:8880/echo" are created.
//    45 connections are rejected by per-renderer WebSocket throttling,
//    and 254 connections to "ws://127.0.0.1:8880/echo" stay pending.
// 3. The handshake to "ws://127.0.0.1:8880/delayed-handshake"
//    (started in Step 1) is completed.
// 4. The handshakes of 254 connections to "ws://127.0.0.1:8880/echo"
//    (created in Step 2) are started and are completed without delay.

for (i = 0; i < socketCount; ++i) {
    var ws = new WebSocket(i == 0 ?
                           "ws://127.0.0.1:8880/delayed-handshake" :
                           "ws://127.0.0.1:8880/echo");

    ws.onopen = function(ws) {
        ++openedCount;
        ws.close();
    }.bind(undefined, ws);

    ws.onclose = function() {
        ++closedCount;
        if (closedCount === socketCount) {
            shouldBeEqualToNumber("openedCount", 255);
            finishJSTest();
        }
    };
}
</script>
